﻿using ACC.Business;
using ACC.Business.Process;
using ACC.Data.Comm;
using ACC.Data.Entities.Common;
using ACC.Data.Entities.Mes;
using ACC.Data.Line;
using ACC.Device;
using ACC.Device.Common;
using ACC.Interface;
using ACC.Interface.Extensions;
using ACC.LineDevice.KukaPack;
using ACC.Util.Restful;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Xml;

namespace Script
{
    /// <summary>
    /// 
    /// </summary>
    public class ProcessClientEx : ProcessClient
    {
        public override bool ExecuteCommand(string userID, string cmdType, string cmdName, System.Xml.XmlDocument xmlDoc, out string msg, out System.Xml.XmlDocument xmlRet)
        {
            return base.ExecuteCommand(userID, cmdType, cmdName, xmlDoc, out msg, out xmlRet);
        }

        /// <summary>
        /// 换型时/客户端启动时返回增加操作指导文件和售后/质量问题
        /// </summary>
        /// <param name="stnData"></param>
        /// <param name="partTypeId"></param>
        /// <returns></returns>
        protected override ClientModelChangeResult StnModelParameters(StnRunningData stnData, uint partTypeId)
        {
            var result = base.StnModelParameters(stnData, partTypeId);
            if (result != null)
            {
                var r1 = new List<Object>();
                var r2 = new List<Object>();
                var param = new Dictionary<string, string>();
                param.Add("line", stnData.StnCfg.LineCfg.Line);
                param.Add("op", stnData.StnCfg.OPCfg.OP);
                param.Add("stn", stnData.StnCfg.Name);
                param.Add("partno", stnData.PartNo == null ? string.Empty : stnData.PartNo.BomCfg.PartNo);
                var apiResult = (LineManager.Lines[stnData.StnCfg.LineCfg.Line].StnProcesses[stnData.StnCfg] as StnProcessKukaPack).PostWebApiMessage("GetWIFiles", null, param);
                if (apiResult.IsSuccess && apiResult.Content != null)
                {
                    var wis = apiResult.Content == null ? null : JsonHelper.ToObject<MESWIHeader>(apiResult.Content.ToString());
                    //var wis = (List<MESWI>)apiResult.Content;
                    if (wis != null)
                    {
                        foreach (var wi in wis.detail)
                        {
                            r1.Add(wi);
                        }
                    }
                    result.Reserved1 = r1;
                }

                apiResult = (LineManager.Lines[stnData.StnCfg.LineCfg.Line].StnProcesses[stnData.StnCfg] as StnProcessKukaPack).PostWebApiMessage("GetQCIssues", null, param);
                if (apiResult.IsSuccess && apiResult.Content != null)
                {
                    var issues = apiResult.Content == null ? null : JsonHelper.ToObject<MESIssueHeader>(apiResult.Content.ToString());
                    //var issues = (List<MESIssue>)apiResult.Content;
                    if (issues != null)
                    {
                        foreach (var issue in issues.detail)
                        {
                            r2.Add(issue);
                        }
                    }
                    result.Reserved2 = r2;
                }

                if (stnData != null && stnData.PartNo != null)
                    result.StatusBits = LineManager.Lines[stnData.StnCfg.LineCfg.Line].StnProcesses[stnData.StnCfg].GetFailureWordCfg(stnData.PartNo.BomCfg, true);
            }

            return result;
        }

        protected override string DoLoad(StnRunningData stnData, uint partTypeId, string[] barcodes, ref ClientResponse clientResponse)
        {
            string msg = string.Empty;
            StnProcessKukaPack stnProcess = (StnProcessKukaPack)LineManager.Lines[stnData.StnCfg.LineCfg.Line].StnProcesses[stnData.StnCfg];
            var dev = stnProcess.Device as ClientCommonDevice;

            if (barcodes != null && barcodes.Length > 0)
            {
                foreach (string barcode in barcodes)
                {
                    string[] lots = barcode.Split(new char[] { '/' });
                    if (lots.Length >= 7)
                    {
                        //var wo = GetWorkOrder(stnData.StnCfg.LineCfg.LineData);
                        //ACCCode rs = stnProcess.HandleLotChange(dev, wo.PONO, barcode, out msg);
                        ACCCode rs = stnProcess.HandleLotChange(dev, "", barcode, out msg);
                        clientResponse.Result = rs == ACCCode.ACCOK;
                        clientResponse.Message = rs == ACCCode.ACCOK ? "扫料成功" : msg;
                        return string.Empty;
                    }
                }
            }

            msg = base.DoLoad(stnData, partTypeId, barcodes, ref clientResponse);
            if (clientResponse.Result)
            {
                var wo = GetWorkOrderByUnitSN(stnData.LastLoadUnit.UnitSN);
                if (wo == null)
                    wo = GetWorkOrder(stnData.StnCfg.LineCfg.LineData);
                clientResponse.Message = wo == null ? string.Empty : JsonHelper.ToJson(wo);

                var lstBoms = new List<ClientBomItems>();
                var vgb = stnData.PartNo.BomCfg.GetBomItemCfgByIDName("GBSN");
                BomItemCfg vmain = null;
                if (stnData.LastLoadUnit != null)
                {
                    var unitIds = stnData.LastLoadUnit.GetUnitIDsByIDValue(stnData.LastLoadUnit.UnitSN);
                    if (unitIds != null && unitIds.Length > 0)
                    {
                        vmain = stnData.PartNo.BomCfg.GetBomItemCfgByIDName(unitIds[0].IDName);
                    }
                }
                if (vmain == null)
                    vmain = stnData.Components.Length > 0 ? stnData.Components[0].Clone() : null;
                if (vmain != null)
                {
                    lstBoms.Add(new ClientBomItems()
                    {
                        PartNo = vmain.BomCfg.PartNo,
                        Rev = vmain.BomCfg.Rev,
                        ItemPartNo = vmain.ItemPartNo,
                        ItemRev = vmain.ItemRev,
                        IDName = vmain.IDName,
                        IDPattern = vmain.IDPattern,
                        Description = vmain.DescLocal,
                        Qty = vmain.QtyPer.ToString(),
                        Sequence = 0,
                        OP = vmain.op,
                        LeadNo = vmain.LeadNo,
                        IDValue = stnData.LastLoadUnit == null ? string.Empty : stnData.LastLoadUnit.UnitSN
                    });
                }
                if (vgb != null && stnData.LastLoadUnit != null)
                {
                    var gb = stnData.LastLoadUnit.GetUnitIDByIDName("GBSN");
                    lstBoms.Add(new ClientBomItems()
                    {
                        PartNo = vgb.BomCfg.PartNo,
                        Rev = vgb.BomCfg.Rev,
                        ItemPartNo = vgb.ItemPartNo,
                        ItemRev = vgb.ItemRev,
                        IDName = vgb.IDName,
                        IDPattern = vgb.IDPattern,
                        Description = vgb.DescLocal,
                        Qty = vgb.QtyPer.ToString(),
                        Sequence = 0,
                        OP = vgb.op,
                        LeadNo = vgb.LeadNo,
                        IDValue = gb == null ? string.Empty : gb.IDValue
                    });
                }

                clientResponse.Data = lstBoms;
            }

            return msg;
        }

        protected override string DoNextStep(StnRunningData stnData, uint partTypeId, string[] barcodes, ref ClientResponse clientResponse)
        {
            string msg = string.Empty;
            StnProcessKukaPack stnProcess = (StnProcessKukaPack)LineManager.Lines[stnData.StnCfg.LineCfg.Line].StnProcesses[stnData.StnCfg];
            var dev = stnProcess.Device as ClientCommonDevice;

            if (barcodes != null && barcodes.Length > 0)
            {
                foreach (string barcode in barcodes)
                {
                    string[] lots = barcode.Split(new char[] { '/' });
                    if (lots.Length >= 7)
                    {
                        //var wo = GetWorkOrder(stnData.StnCfg.LineCfg.LineData);
                        //ACCCode rs = stnProcess.HandleLotChange(dev, wo.PONO, barcode, out msg);
                        ACCCode rs = stnProcess.HandleLotChange(dev, "", barcode, out msg);
                        clientResponse.Result = rs == ACCCode.ACCOK;
                        clientResponse.Message = rs == ACCCode.ACCOK ? "扫料成功" : msg;
                        return string.Empty;
                    }
                }
            }

            msg = base.DoNextStep(stnData, partTypeId, barcodes, ref clientResponse);

            var v = dev.LastNextStepResult;
            if (v != null)
            {
                if (v.ErrorCode == ACCCode.ACCOK)
                {
                    clientResponse.Message = v.ErrorCode == ACCCode.ACCOK ? string.Empty : v.ErrorMessage;

                    var lstBoms = new List<ClientBomItems>();
                    var vgb = stnData.PartNo.BomCfg.GetBomItemCfgByIDName("GBSN");
                    BomItemCfg vmain = null;
                    if (stnData.LastTryNextStepUnit != null)
                    {
                        var unitIds = stnData.LastTryNextStepUnit.GetUnitIDsByIDValue(stnData.LastTryNextStepUnit.UnitSN);
                        if (unitIds != null && unitIds.Length > 0)
                        {
                            vmain = stnData.PartNo.BomCfg.GetBomItemCfgByIDName(unitIds[0].IDName);
                        }
                    }
                    if (vmain == null)
                        vmain = stnData.Components.Length > 0 ? stnData.Components[0].Clone() : null;
                    if (vmain != null)
                    {
                        lstBoms.Add(new ClientBomItems()
                        {
                            PartNo = vmain.BomCfg.PartNo,
                            Rev = vmain.BomCfg.Rev,
                            ItemPartNo = vmain.ItemPartNo,
                            ItemRev = vmain.ItemRev,
                            IDName = vmain.IDName,
                            IDPattern = vmain.IDPattern,
                            Description = vmain.DescLocal,
                            Qty = vmain.QtyPer.ToString(),
                            Sequence = 0,
                            OP = vmain.op,
                            LeadNo = vmain.LeadNo,
                            IDValue = stnData.LastTryNextStepUnit == null ? string.Empty : stnData.LastTryNextStepUnit.UnitSN
                        });
                    }
                    if (vgb != null && stnData.LastTryNextStepUnit != null)
                    {
                        var gb = stnData.LastTryNextStepUnit.GetUnitIDByIDName("GBSN");
                        lstBoms.Add(new ClientBomItems()
                        {
                            PartNo = vgb.BomCfg.PartNo,
                            Rev = vgb.BomCfg.Rev,
                            ItemPartNo = vgb.ItemPartNo,
                            ItemRev = vgb.ItemRev,
                            IDName = vgb.IDName,
                            IDPattern = vgb.IDPattern,
                            Description = vgb.DescLocal,
                            Qty = vgb.QtyPer.ToString(),
                            Sequence = 0,
                            OP = vgb.op,
                            LeadNo = vgb.LeadNo,
                            IDValue = gb == null ? string.Empty : gb.IDValue
                        });
                    }

                    clientResponse.Data = lstBoms;
                }
            }

            return msg;

        }

        protected override string DoPreLoad(StnRunningData stnData, uint partTypeId, String[] barcodes, string idname, ref ClientResponse clientResponse)
        {
            string msg = string.Empty;
            StnProcessKukaPack stnProcess = (StnProcessKukaPack)LineManager.Lines[stnData.StnCfg.LineCfg.Line].StnProcesses[stnData.StnCfg];
            var dev = stnProcess.Device as ClientCommonDevice;

            if (barcodes != null && barcodes.Length > 0)
            {
                foreach (string barcode in barcodes)
                {
                    string[] lots = barcode.Split(new char[] { '/' });
                    if (lots.Length >= 7)
                    {
                        //var wo = GetWorkOrder(stnData.StnCfg.LineCfg.LineData);
                        //ACCCode rs = stnProcess.HandleLotChange(dev, wo.PONO, barcode, out msg);
                        ACCCode rs = stnProcess.HandleLotChange(dev, "", barcode, out msg);
                        clientResponse.Result = rs == ACCCode.ACCOK;
                        clientResponse.Message = rs == ACCCode.ACCOK ? "扫料成功" : msg;
                        return string.Empty;
                    }
                }
            }

            msg = base.DoPreLoad(stnData, partTypeId, barcodes, idname, ref clientResponse);

            var v = dev.LastNextStepResult;
            if (v != null)
            {
                clientResponse.Message = v.ErrorCode == ACCCode.ACCOK ? string.Empty : v.ErrorMessage;

                var lstBoms = new List<ClientBomItems>();
                var vgb = stnData.PartNo.BomCfg.GetBomItemCfgByIDName("GBSN");
                BomItemCfg vmain = null;
                if (stnData.LastTryNextStepUnit != null)
                {
                    var unitIds = stnData.LastTryNextStepUnit.GetUnitIDsByIDValue(stnData.LastTryNextStepUnit.UnitSN);
                    if (unitIds != null && unitIds.Length > 0)
                    {
                        vmain = stnData.PartNo.BomCfg.GetBomItemCfgByIDName(unitIds[0].IDName);
                    }
                }
                if (vmain == null)
                    vmain = stnData.Components.Length > 0 ? stnData.Components[0].Clone() : null;
                if (vmain != null)
                {
                    lstBoms.Add(new ClientBomItems()
                    {
                        PartNo = vmain.BomCfg.PartNo,
                        Rev = vmain.BomCfg.Rev,
                        ItemPartNo = vmain.ItemPartNo,
                        ItemRev = vmain.ItemRev,
                        IDName = vmain.IDName,
                        IDPattern = vmain.IDPattern,
                        Description = vmain.DescLocal,
                        Qty = vmain.QtyPer.ToString(),
                        Sequence = 0,
                        OP = vmain.op,
                        LeadNo = vmain.LeadNo,
                        IDValue = stnData.LastTryNextStepUnit == null ? string.Empty : stnData.LastTryNextStepUnit.UnitSN
                    });
                }
                if (vgb != null && stnData.LastTryNextStepUnit != null)
                {
                    var gb = stnData.LastTryNextStepUnit.GetUnitIDByIDName("GBSN");
                    lstBoms.Add(new ClientBomItems()
                    {
                        PartNo = vgb.BomCfg.PartNo,
                        Rev = vgb.BomCfg.Rev,
                        ItemPartNo = vgb.ItemPartNo,
                        ItemRev = vgb.ItemRev,
                        IDName = vgb.IDName,
                        IDPattern = vgb.IDPattern,
                        Description = vgb.DescLocal,
                        Qty = vgb.QtyPer.ToString(),
                        Sequence = 0,
                        OP = vgb.op,
                        LeadNo = vgb.LeadNo,
                        IDValue = gb == null ? string.Empty : gb.IDValue
                    });
                }

                clientResponse.Data = lstBoms;
            }

            return msg;
        }
    }
}
